# !/usr/bin/python3
# _*_coding: utf-8_*_

import json
import uuid


class DataProcessor(object):

    def __init__(self, filename):
        self._filepath = "data/%s" % filename
        self._data = self.get_data()

    def get_data(self):
        data = []
        with open(self._filepath, "r") as f:
            lines = f.readlines()
            for line in lines:
                data.append(json.loads(line))
        return data

    def process(self):
        # products.csv
        product_file = open('./data/products.csv', 'w')
        product_file.write('pro_id:ID,name,:LABEL\n')
        # brand.csv
        brand_file = open('./data/brand.csv', 'w')
        brand_file.write('brand_id:ID,name,:LABEL\n')
        # talk.csv
        talk_file = open('./data/talk.csv', 'w')
        talk_file.write('talk_id:ID,name,:LABEL\n')
        # attr.csv
        attr_file = open('./data/attr.csv', 'w')
        attr_file.write('attr_id:ID,name,:LABEL\n')
        # pro_brand_rela.csv
        pro_brand_rela_file = open('./data/pro_brand_rela.csv', 'w')
        pro_brand_rela_file.write(':START_ID,:END_ID,:TYPE\n')

        for d in self._data:
            product = d['label']
            product_id = d['item_id']
            product_file.write(
                'product' + str(product_id) + ',' + product + ',Product\n')
            for att in d['attr']:
                if att == '品牌':
                    attr_list = []
                    if not isinstance(d['attr'][att], list):
                        attr_list.append(d['attr'][att])
                    else:
                        attr_list = d['attr'][att]
                    for brand in attr_list:
                        brand_id = uuid.uuid1()
                        brand = brand.replace(',', '')
                        brand_file.write(
                            'brand' + str(brand_id) + ',' + brand + ',Brand\n')
                        pro_brand_rela_file.write(
                            'product' + str(product_id) + ',' + 'brand' + str(brand_id) + ',hasBrand\n')
                        pro_brand_rela_file.write(
                            'brand' + str(brand_id) + ',' + 'product' + str(product_id) + ',hasProduct\n')
                elif att == '大家说':
                    attr_list = []
                    if not isinstance(d['attr'][att], list):
                        attr_list.append(d['attr'][att])
                    else:
                        attr_list = d['attr'][att]
                    for talk in attr_list:
                        talk_id = uuid.uuid1()
                        talk = talk.replace(',', '')
                        talk = talk.strip()
                        talk_file.write('talk' + str(talk_id) +
                                        ',' + talk + ',Talk\n')
                        pro_brand_rela_file.write(
                            'product' + str(product_id) + ',' + 'talk' + str(talk_id) + ',hasRsv\n')
                else:
                    attr_list = []
                    if not isinstance(d['attr'][att], list):
                        attr_list.append(d['attr'][att])
                    else:
                        attr_list = d['attr'][att]
                    for at in attr_list:
                        attr_id = uuid.uuid1()
                        at = at.strip()
                        at = at.replace(',', '')
                        attr_file.write('attr' + str(attr_id) +
                                        ',' + at + ',Attribute\n')
                        att = att.replace('：','')
                        pro_brand_rela_file.write(
                            'product' + str(product_id) + ',' + 'attr' + str(attr_id) + ',' + att + '\n')
        print("插入成功")
        product_file.close()
        brand_file.close()
        talk_file.close()
        attr_file.close()
        pro_brand_rela_file.close()

if __name__ == "__main__":
    dp = DataProcessor("products_more")
    dp.process()
